#ifndef __MY_CONFIG_H__
#define __MY_CONFIG_H__

#include "util.hpp"
#include <mutex>
namespace vic_cloud
{
#define CONFIG_FILE "./cloud.conf"
    class Config
    {

    private:
        static std::mutex _mutex;
        static Config *_instance;
        // 构造函数内，加载配置文件，（读取配置文件数据进行解析，将数据存放到私有成员里面）
        Config()
        {
           ReadConfigFile();
        }

    public:
       // bool ReadConfig(const std::string &filename);
        int GetHotTime()
        {
            return _hot_time;
        }
        int GetServerPort()
        {
            return _server_port;
        }
        std::string GetServerIp()
        {
            return _server_ip;
        }
        std::string GetDownloadPrefix()
        {
            return _download_prefix;
        }
        std::string GetPackFileSuffix()
        {
            return _packfile_suffix;
        }
        std::string GetPackDir()
        {
            return _pack_dir;
        }
        std::string GetBackDir()
        {
            return _back_dir;
        }
        std::string GetBackupFile()
        {
            return _backup_file;
        }
        // 读取配置文件
        bool ReadConfigFile()
        {
           // printf("12");
            FileUtil f("./cloud.conf");
            std::string body;
            if (f.GetContent(&body) == false)
            {
                std::cout << "load config failed\n";
                return false;
            }
            Json::Value root;
            if (JsonUtil::UnSerialize(body, &root) == false)
            {
                std::cout << "parse config failed\n";
                return false;
            }
            _hot_time = root["hot_time"].asInt();
            _server_port = root["server_port"].asInt();
            _server_ip = root["server_ip"].asString();
            _download_prefix = root["download_prefix"].asString();
            _packfile_suffix = root["packfile_suffix"].asString();
            _pack_dir = root["pack_dir"].asString();
            _back_dir = root["back_dir"].asString();
            _backup_file = root["backup_file"].asString();
            
            return true;
        }

    public:
        static Config *GetInstance()
        {
            if (_instance == NULL)
            {

                _mutex.lock();
                if (_instance == NULL)
                {
                    _instance = new Config();
                }
                _mutex.unlock();
            }
            return _instance;
        }

    private:
        time_t _hot_time;             // 熱點判斷時間
        int _server_port;             // 服务器监听端口
        std::string _server_ip;       // 服务器ip
        std::string _download_prefix; // 下载的url前缀路径
        std::string _packfile_suffix; // 压缩包的后缀
        std::string _pack_dir;        // 压缩包存放路径
        std::string _back_dir;        // 备份文件存放路径
        std::string _backup_file;    // 备份信息的存放文件
    };
    Config *Config::_instance = NULL;
    std::mutex Config::_mutex;
}
#endif